package br.com.yurikimo.ykkit;

import java.util.Vector;

//import android.app.Activity;
//import android.content.Intent;
//import android.net.Uri;
//import android.view.Gravity;
//import android.view.MotionEvent;
//import android.view.View;
import android.widget.RelativeLayout;

public abstract class YKToolBar extends RelativeLayout{
	private LayoutParams layoutParams;
	private RelativeLayout relativeLayout = null;
	private float x = 0;
	private float y = 0;
	private float width = 100;
	private float height = 50;
	//private int gravity = Gravity.CENTER;
	private Vector<YKButton> vctButton = new Vector<YKButton>();
	
	public YKToolBar(int gravity){
		super(YKDirector.getActivity());
		//this.gravity = gravity;
		
		init();
	}

	private void init(){

	}
	
	public void setBounds(float x, float y, float width, float height){
		layoutParams = null;
		
		layoutParams = new RelativeLayout.LayoutParams((int)width, (int)height);
		
		layoutParams.leftMargin = (int)x;
		layoutParams.topMargin = (int)y;		
		
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	protected void refreshLayout(){
		setBounds(x, y, width, height);
		
		if (relativeLayout != null){
			if (relativeLayout.indexOfChild(this) >= 0){
				relativeLayout.removeView(this);
				
				relativeLayout.addView(this, layoutParams);
				relativeLayout.invalidate();
			}
		}
	}
	
	public void setPosition(float x, float y){
		this.x = x;
		this.y = y;
	
		refreshLayout();
	}
	
	public float getObjectWidth(){
		return width;
	}
	
	public float getObjectHeight(){
		return height;
	}
	
	public float getObjectX(){
		return x;
	}
	
	public float getObjectY(){
		return y;
	}
	
	public void uncheckButtons(){
		for (int i = 0; i < getChildCount(); i++){
			YKButton btnTemp = (YKButton)getChildAt(i);
			
			btnTemp.chek(false);
		}
	}
	
	public void createButtons(int number){
//		removeAllViews();
//		
//		float width = getObjectWidth()/ number;
//		float height = getObjectHeight();
//		float x = 0;
//		float y = 0;
//
//		for (int i = 0; i < number; i++){
//			YKButton btnTemp = new YKButton("" + i, getContext()){
//				@Override
//				public void onClick(YKButton button){
//					Intent intent = new Intent(Intent.ACTION_VIEW);
//				  intent.setData(Uri.parse(url));
//				  intent.addCategory(Intent.CATEGORY_BROWSABLE); 
//				  ((Activity)getContext()).startActivity(intent);
//				}
//			};	
//			x = 2 + (width * i);
//			btnTemp.setBounds(x, y, width, height);
//			btnTemp.setGravity(gravity);
//			btnTemp.addToLayout(this);
//			btnTemp.setTag("" + i);
//			
//			btnTemp.setOnTouchListener(new OnTouchListener()
//			{
//				public boolean onTouch(View arg0, MotionEvent arg1)
//				{
//					uncheckButtons();
//					
//					((YKButton)arg0).chek();
//					
//					if (arg1.getAction() == MotionEvent.ACTION_DOWN){
//						onButtonClicked(Integer.parseInt(arg0.getTag().toString()));
//					}
//          return true;
//				}
//			});	
//			
//			vctButton.add(btnTemp);
//		}
	}
	
	public abstract void onButtonClicked(int buttonIndex);
	
	public YKButton getButton(int index){
		return vctButton.elementAt(index);
	}
	
	public void addToLayout(RelativeLayout relativeLayout){
		this.relativeLayout = relativeLayout;
		
		relativeLayout.addView(this, layoutParams);
		relativeLayout.invalidate();
	}
}
